﻿using Gateway.Common.Result;

namespace Gateway.Common.Extensions
{
    /// <summary>
    /// 递归服务类
    /// </summary>
    public class TreeSelectExtension
    {
        /// <summary>
        /// 递归公共方法
        /// </summary>
        /// <typeparam name="T">要递归的类</typeparam>
        /// <param name="menus">要递归的类集合</param>
        /// <param name="parentId">递归的根节点</param>
        /// <returns>递归后的集合</returns>
        public static List<T> HandleTreeChildren<T>(List<T> menus, string parentId) where T : TreeSelectResult<T>
        {
            var returnList = new List<T>();
            var list = menus.Where(x => x.ParentId == parentId).OrderBy(x => x.SortNumber).ToList();
            if (list.Any())
            {
                foreach (var item in list)
                {
                    item.Children = HandleTreeChildren(menus, item.Id);
                    returnList.Add(item);
                }
            }

            return returnList;
        }
    }
}